#include "pipsutil.h"

namespace UTIL {

void initPips()
{
    FILE *file = fopen("pips.txt", "r");
    if (!file) {
        generatePips();
        file = fopen("pips.txt", "r");
        if (!file)
            return;
    }
    char symbol[100];
    double pips;
    while (fscanf(file, "%s %lf", symbol, &pips) != EOF) {
        mapPips[symbol] = pips;
    }
    fclose(file);
}

double getPips(const std::string &symbol)
{
    if (mapPips.find(symbol) != mapPips.end()) {
        return mapPips[symbol];
    }
    printf("Warning: %s not found pips\n", symbol.c_str());
    return mapPips["DEFAULT"];
}

void generatePips()
{
    FILE *file = fopen("pips.txt", "w");
    if (!file) {
        return;
    }
    fprintf(file, "%s %lf\n", "EURUSD", 0.0001);
    fprintf(file, "%s %lf\n", "GBPUSD", 0.0001);
    fprintf(file, "%s %lf\n", "USDJPY", 0.01);
    fprintf(file, "%s %lf\n", "USDCHF", 0.0001);
    fprintf(file, "%s %lf\n", "XAUUSD", 0.01);
    fprintf(file, "%s %lf\n", "GOLD", 0.01);
    fprintf(file, "%s %lf\n", "DEFAULT", 0.0001);
    fclose(file);
}

};